﻿using IdentityServer.Infrastructure;
using MediatR;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Storage;
using RepositoryLib;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using WeChatIdentity.EntityConfigurations;
using WeChatIdentity.Models;

namespace IdentityServer
{
    public class IdentityServerContext : IdentityDbContext<ApplicationUser>
    {
        public IdentityServerContext(DbContextOptions<IdentityServerContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
        }
    }
    public class IdentityServerContextDesignFactory : IDesignTimeDbContextFactory<IdentityServerContext>
    {
        public IdentityServerContext CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<IdentityServerContext>()
                .UseSqlServer("Server=.;Initial Catalog=IdentityServerDb;Integrated Security=true");

            return new IdentityServerContext(optionsBuilder.Options);
        }

        class NoMediator : IMediator
        {
            public Task Publish<TNotification>(TNotification notification, CancellationToken cancellationToken = default(CancellationToken)) where TNotification : INotification
            {
                return Task.CompletedTask;
            }

            public Task Publish(object notification, CancellationToken cancellationToken = default)
            {
                return Task.CompletedTask;
            }

            public Task<TResponse> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = default(CancellationToken))
            {
                return Task.FromResult<TResponse>(default(TResponse));
            }
            public Task<object> Send(object request, CancellationToken cancellationToken = default)
            {
                return Task.FromResult<object>(request);
            }
        }
    }
}
